package com.johnlee.sentinel_quick_start.controller;

import com.johnlee.sentinel_quick_start.service.TestService_01;
import com.alibaba.csp.sentinel.AsyncEntry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController_04 {

    @Autowired
    TestService_01 testService_01;

    @GetMapping("helloAsync")
    public String helloAsync() {
        AsyncEntry asyncEntry = null;
        try {
            asyncEntry = SphU.asyncEntry("Sentinel_Async"); // 限流入口
            testService_01.helloAsync();
            return "Async Done";
        } catch (BlockException e) { // 被限流或者降级处理
            return "Busy";
        } finally {
            if(asyncEntry != null) {
                asyncEntry.exit();
            }
        }
    }
}
